package com.awesome.todo.activities;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.awesome.todo.R;
import com.awesome.todo.Task;
import com.awesome.todo.database.TaskDataSource;

/**
 * This activity is used to create a new task.
 * 
 * @author Kelley Sheffield, Syneva Runyan
 * 
 */
public class NewTaskActivity extends Activity {
	public static EditText nameEdit;
	public static EditText dateEdit;
	public static EditText addressEdit;
	public static EditText cityEdit;
	public static EditText stateEdit;
	public static EditText descriptionEdit;
	public static Spinner categorySpinner;
	public static Button newTaskButton;
	public static Button cancelButton;
	public static long currentUserId;
	int taskYear;
	int taskMonth;
	int taskDay;
	static final int DATE_DIALOG_INT = 0;
	final String[] categoryArray = {"School", "Personal", "Work", "None"};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newtask);
		if (getIntent().getExtras() != null)
			currentUserId = getIntent().getExtras().getLong("userId");
		
		final TaskDataSource TaskTable = new TaskDataSource(this.getApplicationContext());

		nameEdit = (EditText) findViewById(R.id.nameEditText);
		dateEdit = (EditText) findViewById(R.id.dateEditText);
		addressEdit = (EditText) findViewById(R.id.addressEditText);
		cityEdit = (EditText) findViewById(R.id.cityEditText);
		stateEdit = (EditText) findViewById(R.id.stateEditText1);
		descriptionEdit = (EditText) findViewById(R.id.descriptionEditText);
		categorySpinner = (Spinner) findViewById(R.id.categorySpinner);
		newTaskButton = (Button) findViewById(R.id.newTaskButton);
		cancelButton = (Button) findViewById(R.id.cancelButton);
		
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryArray);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		categorySpinner.setAdapter(spinnerAdapter);
		
		final Calendar cal = Calendar.getInstance();
		taskYear = cal.get(Calendar.YEAR);
		taskMonth = cal.get(Calendar.MONTH);
		taskDay = cal.get(Calendar.DAY_OF_MONTH);
		dateEdit.setClickable(true);
		dateEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View arg0, boolean changed) {
				if (changed)
					showDialog(DATE_DIALOG_INT);
			}
			
		});
		
		newTaskButton.setOnClickListener(new OnClickListener() {

			/**
			 * This is called when the user clicks the "create" button. It
			 * verifies that the entered values are valid and then creates and
			 * stores the new Task.
			 */
			public void onClick(View arg0) {

				String name = nameEdit.getText().toString().trim();
				String date = dateEdit.getText().toString().trim();
				String address = addressEdit.getText().toString().trim();
				String city = cityEdit.getText().toString().trim();
				String state = stateEdit.getText().toString().trim();
				String description = descriptionEdit.getText().toString().trim();
				String selectedCategory = categorySpinner.getSelectedItem().toString();
				String type = (selectedCategory.equals("None") ? ("NULL") : (selectedCategory));
				// VERIFY VALUES ARE ACCURATE

				Task newTask = TaskTable.createTask(name, date, address, city, state, description, type, currentUserId);
				if (newTask != null) {
					Log.i("NewTaskActivity", "Task successfully created");
				} else {
					Log.i("NewTaskActivity", "Task creation failed");
				}
				Log.i("ONCLICK", "CLICKED");
				finish();

			}

		});

		cancelButton.setOnClickListener(new OnClickListener() {
			/**
			 * Cancels new Task and closes the activity.
			 */
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			taskYear = year;
			taskMonth = monthOfYear + 1;
			taskDay = dayOfMonth;
			String taskMonthString, taskDayString, taskYearString = "";
			if (taskMonth < 10) {
				taskMonthString = "0" + taskMonth;
			} else {
				taskMonthString = "" + taskMonth;
			}
			if (taskDay < 10) {
				taskDayString = "0" + taskDay;
			} else {
				taskDayString = "" + taskDay;
			}
			taskYearString = "" + taskYear;
			String taskDateString = "" + taskMonthString + "/" + taskDayString + "/" + taskYearString;
			dateEdit.setText(taskDateString);
		}
	};
	
	protected Dialog onCreateDialog(int id) {
		
		switch(id) {
		case DATE_DIALOG_INT:
			return new DatePickerDialog(this, dateSetListener, taskYear, taskMonth, taskDay);
		}
		return null;
		
	}
}
